package net.chensee.avastxa.logic;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import net.chensee.avastxa.common.socket.SocketServerStarter;
import net.chensee.avastxa.logic.handler.HttpServerInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 逻辑层
 * 提供登录、消息路由
 */
public class LogicStarter {

    private static final Logger logger = LoggerFactory.getLogger(LogicStarter.class);

    public static void start() throws InterruptedException {
        ChannelHandler channelHandler = new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                // server端发送的是httpResponse，所以要使用HttpResponseEncoder进行编码
                socketChannel.pipeline().addLast(new HttpResponseEncoder());
                // server端接收到的是httpRequest，所以要使用HttpRequestDecoder进行解码
                socketChannel.pipeline().addLast(new HttpRequestDecoder());
                socketChannel.pipeline().addLast(new HttpServerInboundHandler());
            }
        };

        SocketServerStarter.newInstance(128, 8080).setInitialzer(channelHandler).start();
    }

}
